package com.easy.image.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.easy.common.CommonResult;
import com.easy.image.config.EasyConfg;

@RestController
@RequestMapping("image")
@CrossOrigin
public class ImageController {
	@PostMapping("upload")
	public CommonResult  uploadImage(@RequestParam("file") MultipartFile file) {
		//获取文件后缀名
		String oldName=file.getOriginalFilename();
		String filetype=oldName.substring(oldName.lastIndexOf("."));
		//生成新文件
		String newName=UUID.randomUUID().toString()+filetype;
		
		String newFilePath=EasyConfg.STATIC_DIR+newName;
		
		File newFile=new File(newFilePath);
		//流传递文件
		try {
			file.transferTo(newFile);
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		//新的访问路径
		String url=EasyConfg.STATIC_PATH+newName;
		return CommonResult.success(url);
	}

}
